/* List clicked View */
function getAccountInfo(id){
	var url = "getAccountInfo_linux.jsp";
	var param = {
		work_no : id
	};
	callAjax(url,param, "ACCOUNT_VIEW");
}

/* callAjax successAjax */
function successAjax(data, serviceName){
	if(serviceName == "ACCOUNT_VIEW"){
		/* Info value setting */
		$('#system_name').val(data.rows[0].system_name);
		$('#id').val(data.rows[0].id);
		$('#group').val(data.rows[0].group);
		$('#full_dir').val(data.rows[0].full_dir);
		$('#login_shell').val(data.rows[0].login_shell);
		$('#pw_max_used_date').val(data.rows[0].pw_max_used_date);
		$('#pw_min_used_date').val(data.rows[0].pw_min_used_date);
		$('#pw_expire_alarm_date').val(data.rows[0].pw_expire_alarm_date);
		$('#expire_block_date').val(data.rows[0].expire_block_date);
		$('#lock_status').val(data.rows[0].lock_status);
		$('#account_status').val(data.rows[0].account_status);
		$('#last_connect_gear').val(data.rows[0].last_connect_gear);
		$('#pw_change_date').val(data.rows[0].pw_change_date);
		$('#last_sync_date').val(data.rows[0].last_sync_date);
	}else if(serviceName == "ACCOUNT_DEL"){
		alert(data.result);
	}else if(serviceName == "ACCOUNT_INFO"){
		alert(data.result);
	}
}

/* JqGrid Account List */
function initGridAccount() {
	$("#gridList").jqGrid({
		url:'getJqgridAccountList_linux.jsp',
		mtype: 'post',
		//postData: {  },
		datatype: "json",
		colNames:['시스템명','계정','상태','잠금','만료일자','동기화시간'],
		colModel:[
				{name:'system_name',index:'system_name', width:50,align:"right"},
				{name:'account',index:'account', width:80,align:"left"},
				{name:'status',index:'status', width:180,align:"left"},
				{name:"lock",index:'lock', width:100,align:"right"},
				{name:'expire_date',index:'expire_date', width:50,align:"right"},
				{name:'sync_time',index:'sync_time', width:50,align:"right"}
			],
		rowList: [0,5,10,15,20],
		rowNum:5,
		height:240,
		autowidth : true,
		sortname: "work_no",
		sortorder: 'DESC',
		viewrecords: true,
		multiselect: true,//앞에 체크박스처리
		multikey: "",//multikey: "ctrlKey/shiftKey",
		pager: '#pager_id',
		//editurl: "server.json",
      
		onSelectRow: function(id){  //row가 선택되었을 경우
			getAccountInfo(id);
		},
		loadComplete : function(data){
			pagecount = data.total;
			$("#pager").pager({ pagenumber: data.page, pagecount: pagecount, buttonClickCallback: PageClick });
        },
		loadError : function(xhr,st,err) {
			//jQuery("#rsperror").html("Type: "+st+"; Response: "+ xhr.status + " "+xhr.statusText);
		},
		emptyrecords: "데이터가 존재하지 않습니다.",
		caption:'테이블',
		jsonReader: {
			repeatitems:false
		}
	});
	$("#gridList").jqGrid('navGrid','#pager_id',{edit:false,add:false,del:false,search:true});
}

/* JqGrid AccountLog List */
function initGridAccountLog() {
	$("#gridList2").jqGrid({
		url:'getJqgridAccountLogList_linux.jsp',
		mtype: 'post',
		//postData: {  },
		datatype: "json",
		colNames:['기간','구분','작업','결과','중요도','작업자','원격지'],
		colModel:[
				{name:'calender',index:'calender', width:50,align:"right",key:true},
				{name:'type',index:'type', width:80,align:"left"},
				{name:'work',index:'work', width:180,align:"left"},
				{name:"result",index:'result', width:100,align:"right"},
				{name:'important',index:'important', width:50,align:"right"},
				{name:'worker',index:'worker', width:50,align:"right"},
				{name:'ip_addr',index:'ip_addr', width:50,align:"right"}
			],
		//loadonce:true,
		rowList: [0,5,10,15,20],
		rowNum:5,
		height:130,
		autowidth : true,
		sortname: "calender",
		sortorder: 'DESC',
		viewrecords: true,
		multiselect: false,//앞에 체크박스처리
		multikey: "",//multikey: "ctrlKey/shiftKey",
		pager: '#pager_id',
		//editurl: "server.json",
      
		onSelectRow: function(id){  //row가 선택되었을 경우
		},
		loadComplete : function(data){
			pagecount2 = data.total;
			$("#pager2").pager({ pagenumber: data.page, pagecount: pagecount2, buttonClickCallback: PageClick2 });
        },
		loadError : function(xhr,st,err) {
			//jQuery("#rsperror").html("Type: "+st+"; Response: "+ xhr.status + " "+xhr.statusText);
		},
		emptyrecords: "데이터가 존재하지 않습니다.",
		caption:'테이블',
		jsonReader: {
			repeatitems:false
		}
	});
	$("#gridList2").jqGrid('navGrid','#pager_id',{edit:false,add:false,del:false,search:true});
}

var pagecount;
var pagecount2;

$(document).ready(function(){
	initGridAccount();
	initGridAccountLog();
	initButton();
});
PageClick = function(pageclickednumber) {
    $("#pager").pager({ pagenumber: pageclickednumber, pagecount: pagecount, buttonClickCallback: PageClick });
    $("#gridList").setGridParam({ postData: { pagePlug: pageclickednumber} }).trigger("reloadGrid");  // Postdata을 넘겨주고 url로 지정된 ajax 호출하고 Grid을 Relo
};
PageClick2 = function(pageclickednumber) {
    $("#pager2").pager({ pagenumber: pageclickednumber, pagecount: pagecount2, buttonClickCallback: PageClick2 });
    $("#gridList2").setGridParam({ postData: { pagePlug: pageclickednumber} }).trigger("reloadGrid");  // Postdata을 넘겨주고 url로 지정된 ajax 호출하고 Grid을 Relo
};

function initButton() {
	var link = getUrl();
	//이동,암호변경,보안암호변경,추가,삭제 클릭 이벤트
	$("ul.btn_box > li > a").each(function( index ) {
		$(this).on("click", function(event){
			var name = $(this).attr("id");
			var param = {
					type : gid
				};

			/* 비밀번호 변경 */
			if( name == "btn_change_pw" ) {
				openModal(link+"account_change_passwd.jsp", param, 410, 0);
			/* popup창 */
			} else if( name == "btn_add" ) {
				openModal(link+"account_linux_add.jsp", param, 410, 0);
			/* checkbox checked 삭제 */
			} else if( name == "btn_del" ) {
				 var gridSelRow = $("#gridList").getGridParam('selrow');//마지막 클릭한 row
				 var param = {
						 work_no : gridSelRow
				 };
				 
				 if(gridSelRow != null){
					 callAjax("deleteAccount.jsp", param, "ACCOUNT_DEL");
				 }else{
					 alert("checkbox에 한개 이상 선택 되어야 합니다");
				 }
			/* checkbox checked 삭제 */
			} else if( name == "btn_del2" ) {
				 var gridSelRow = $("#gridList").getGridParam('selrow');//마지막 클릭한 row
				 var param = {
						 work_no : gridSelRow
				 };
				 
				 if(gridSelRow != null){
					 callAjax("deleteAccount.jsp", param, "ACCOUNT_DEL");
				 }else{
					 alert("checkbox에 한개 이상 선택 되어야 합니다");
				 }
			} else if( name == "btn_edit" ) {
				/* 작업 상세보기에서 저장*/
				 var gridSelRow = $("#gridList").getGridParam('selrow');//마지막 클릭한 row
				 var param = {
						 work_no : gridSelRow
				 };
				 
				 if(gridSelRow != null){
					 var del_msg = confirm("저장하시겠습니까?");
						if( del_msg ) {
							 callAjax("insertAccountInfo.jsp", param, "ACCOUNT_INFO");
						}
					
				 }else{
					 alert("checkbox에 한개 이상 선택 되어야 합니다")
				 }
			}
		});
	});
}
